<?php
/**
 * vanephp web framework
 * 上传文件助手
 *
 * @author aray <ofree@qq.com>
 * @link http://www.vanephp.com/s
 * @copyright Copyright &copy; 2010 aray
 * @license http://www.vanephp.com/license
 * @package helper
 */

if ( ! defined ( 'INVANE' ) )
	exit ( '!' );

class vane_helper_fileuploader {
	
	public $allowExt = null;
	public $denyExt = null;
	public $getPhysicalPath = true;
	
	function upload ( $fileField, $dir, & $getFilesize = null, $allowExt = null, $denyExt = null ) {
		
		if ( ! file_exists ( $dir ) ) {
			mkdirs ( $dir );
		}
		
		if ( isset ( $_FILES[$fileField] ) && ! empty ( $_FILES[$fileField] ) ) {
			$postFile = $_FILES[$fileField];
			if ( is_array ( $postFile ) && ! empty ( $postFile ) && ! empty ( $postFile['name'] ) && is_uploaded_file ( $postFile['tmp_name'] ) ) {
				
				// 扩展名
				$ext = strrchr ( $postFile['name'], '.' );
				$ext = strtolower ( $ext );
				
				if ( $ext == '' ) {
					return (json_encode ( array (
						'err' => '1', 'msg' => '未知的文件类型' 
					) ));
				}
				
				// 类型检查
				// 允许类型
				if ( $allowExt == null ) {
					if ( $this->allowExt ) {
						$allowExt = explode ( ';', $this->allowExt );
					} else {
						$allowExt = explode ( ';', config::get ( 'upload_allow_extname' ) );
					}
				} else {
					$allowExt = explode ( ';', $allowExt );
				}
				// 禁止类型
				if ( $denyExt == null ) {
					if ( $this->denyExt ) {
						$denyExt = explode ( ';', $this->denyExt );
					} else {
						$denyExt = explode ( ';', config::get ( 'upload_deny_extname' ) );
					}
				} else {
					$denyExt = explode ( ';', $denyExt );
				}
				
				// 禁止上传0字节文件
				if ( $postFile['size'] <= 0 ) {
					return (json_encode ( array (
						'err' => '1', 'msg' => '文件0字节，禁止上传' 
					) ));
				}
				
				// 文件大小检查
				$maxsize = 0;
				if ( config::get ( 'upload_maxsize' ) == 0 ) {
					$maxsize = ini_get ( 'upload_max_filesize' );
				} else {
					$maxsize = config::get ( 'upload_maxsize' );
				}
				
				if ( strtolower ( substr ( $maxsize, - 1, 1 ) ) == 'm' ) {
					$maxsize *= (1024 * 1024);
				}
				
				if ( $postFile['size'] > $maxsize ) {
					return (json_encode ( array (
						'err' => '1', 'msg' => '文件超过允许范围，禁止上传' 
					) ));
				}
				
				// 禁止类型
				if ( ! in_array ( $ext, $denyExt ) && in_array ( $ext, $allowExt ) ) {
					
					$err = '';
					switch ( $postFile['error'] ) {
						case '1' :
							$err = '文件超过系统限制';
							break;
						case '2' :
							break;
						case '3' :
							break;
						case '4' :
							$err = '没有上传文件';
							break;
						case '6' :
							$err = '上传临时文件股权不存在';
							break;
						case '7' :
							$err = '写文件失败';
							break;
						case '8' :
							$err = '禁止的文件类型';
							break;
						case '0' :
							$filename = time ( ) . mt_rand ( 10000, 99999 );
							$filename .= $ext;
							$filePath = $dir . DS . $filename;
							if ( move_uploaded_file ( $postFile['tmp_name'], $filePath ) ) {
								$getFilesize = $postFile['size'];
								$f = str_replace ( DS, '/', $filePath );
								
								$physicalPath = preg_replace ( '/\/+/', '/', $f );
								if ( $this->getPhysicalPath ) {
									return (json_encode ( array (
										'err' => '0', 'msg' => $physicalPath 
									) ));
								} else {
									return (json_encode ( array (
										'err' => '0', 'msg' => str_replace ( $_SERVER['DOCUMENT_ROOT'], '', $physicalPath ) 
									) ));
								}
							}
							$err = '移动上传文件失败';
							break;
						default :
							$err = '系统错误';
							break;
					}
					
					// 处理错误
					if ( $err != '' ) {
						return (json_encode ( array (
							'err' => $err, 'msg' => $err 
						) ));
					}
				} else {
					return (json_encode ( array (
						'err' => '1', 'msg' => '禁止的文件类型 ' . $ext 
					) ));
				}
			}
		}
		return (json_encode ( array (
			'err' => '1', 'msg' => '没有文件上传' 
		) ));
	}
}